package com.testDemo.guava;

import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Sets;
import com.testDemo.guava.bean.Student;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.math.BigDecimal;
import java.util.*;

import static com.google.common.collect.Sets.newHashSet;

/**
 * @Auther: zouren
 * @Date: 2019/4/9 14:50
 * @Description:
 */
public class SetsTest {

	Random random;
	List<Student> stuList;

	List<String> wordList;

	Student[] students;
	List<Map<String, BigDecimal>> mapList;

	HashSet<Integer> setA ;
	HashSet<Integer> setB ;

	ImmutableSet charListA ;
	ImmutableSet charListB ;
	ImmutableSet charListC ;


	@BeforeEach
	public void init() {
		charListA = ImmutableSet.of("a1", "a2");
		charListB = ImmutableSet.of("B1", "B2", "B3");
		charListC = ImmutableSet.of("C1", "C2", "C3","C4");
		setA = newHashSet(1, 2, 3, 4, 5);
		setB = newHashSet(4, 5, 6, 7, 8);
		random = new Random();
		stuList = new ArrayList<Student>() {
			{
//                for (int i = 0; i < 3; i++) {
//                    add(new Student("student" + i, random.nextInt(50) + 50));
//                    add(new Student("aa", random.nextInt(50) + 50));
//                }
				add(new Student("student" + 1, random.nextInt(50) + 50, new BigDecimal(1 + "")));
				add(new Student("student" + 2, random.nextInt(50) + 50, new BigDecimal(20.2 + "")));
				add(new Student("student" + 3, random.nextInt(50) + 50, new BigDecimal(300.03 + "")));

			}
		};
		wordList = new ArrayList<String>() {
			{
				add("a");
				add("b");
				add("c");
				add("d");
				add("e");
				add("f");
				add("g");
			}
		};
		Map<String, BigDecimal> row = new HashMap<>();
		row.put("a", new BigDecimal("1"));
		Map<String, BigDecimal> row2 = new HashMap<>();
		row2.put("a", new BigDecimal("2"));
		mapList = new ArrayList<Map<String, BigDecimal>>() {
			{

				add(row);
				add(row2);

			}
		};
	}


	public static void main(String[] args) {

       	HashSet<Integer> setA = newHashSet(1, 2, 3, 4, 5);
       	HashSet<Integer> setB = newHashSet(4, 5, 6, 7, 8);

       	Sets.SetView<Integer> union = Sets.union(setA, setB);
       	System.out.println("union:");//12345867
       	for (Integer integer : union)
             System.out.print(integer);

       	Sets.SetView<Integer> difference = Sets.difference(setA, setB);
		System.out.println();
       	System.out.println("difference:");//1,2,3,
       	for (Integer integer : difference)
            	    System.out.print(integer+",");

       	Sets.SetView<Integer> intersection = Sets.intersection(setA, setB);
		System.out.println();
       	System.out.println("intersection:");//45
       	for (Integer integer : intersection)
            	    System.out.print(integer);
    }

    @Test
    public void test1(){
    	Map m = new HashMap();
    	m.put("a",new String[]{"a","b","c"});
    	Object required = m.get("a");
		Set<String> requireds = Sets.newHashSet((String[])required);
		System.out.println(requireds.contains("b"));
		wordList.add("cc");
		wordList.add("cc");
		wordList.add("bb");
		wordList.add("bb");
		System.out.println(wordList);//[a, b, c, d, e, f, g, cc, cc, bb, bb]
		HashSet<String> strings = newHashSet(wordList);
		System.out.println(strings);//[cc, bb, a, b, c, d, e, f, g]
	}
	@Test
	public void cartesianProductTest(){
		Set set = Sets.cartesianProduct(charListA, charListB, charListC);
		set.stream().forEach(row->System.out.println(row));

	}
	@Test
	public void cartesianProductObjectTest(){

		Set setA = new HashSet() {
			{
//                for (int i = 0; i < 3; i++) {
//                    add(new Student("student" + i, random.nextInt(50) + 50));
//                    add(new Student("aa", random.nextInt(50) + 50));
//                }
				add(new Student("student" + 1, random.nextInt(50) + 50, new BigDecimal(1 + "")));
				add(new Student("student" + 2, random.nextInt(50) + 50, new BigDecimal(20.2 + "")));
				add(new Student("student" + 3, random.nextInt(50) + 50, new BigDecimal(300.03 + "")));

			}
		};
		Set setB = new HashSet() {
			{
				add(new Student("studentB" + 1,  random.nextInt(50) + 50, new BigDecimal(1 + "")));
				add(new Student("studentB" + 2, random.nextInt(50) + 50, new BigDecimal(20.2 + "")));
				add(new Student("studentB" + 3, random.nextInt(50) + 50, new BigDecimal(300.03 + "")));

			}
		};
		Set setC = new HashSet() {
			{
				add(new Student("studentC" + 1,  random.nextInt(50) + 50, new BigDecimal(1 + "")));
				add(new Student("studentC" + 2, random.nextInt(50) + 50, new BigDecimal(20.2 + "")));

			}
		};
		Set set = Sets.cartesianProduct(setA, setB, setC);
		set.stream().forEach(row->System.out.println(row));

	}


}
